// pages/mch/car/stop.js 
const App = getApp()
import Util from '../../../utils/util'
import Kits from '../../../utils/kits'

Page({
  data: {
    init: false,
    loading: false,
    CustomBar: App.Global.CustomBar,

    cid: '',
    items: [],

    add: false,
    dtimes: [],
    minDate: undefined,
    maxDate: undefined,
    range: [undefined, undefined],
    begin: {
      value: undefined,
      year: '',
      date: '',
      time: '',
    },
    close: {
      value: undefined,
      year: '',
      date: '',
      time: '',
    },
    formatter: (day) => {
      if (day.type === 'start') {
        day.bottomInfo = '开始'
      } else if (day.type === 'end') {
        day.bottomInfo = '结束'
      } else if (day.type === 'start-end') {
        day.bottomInfo = '开/结'
      }
      return day
    },

  },
  onLoad(options) {
    this.setData({
      cid: options.cid
    })
    this.execLoad()
  },

  execLoad() {
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/merchant/car/detail',
      body: {
        id: this.data.cid
      },
      fnSucc: resp => {
        this.setData({
          init: true,
          loading: false,
          items: resp.data.stops
        })
      },
    })
  },
  showAdd() {
    this.doReset()
    App.doPost({
      uri: '/app/base/biz/car/available',
      body: {
        id: this.data.cid,
      },
      fnSucc: resp => {
        let rdpStr = Kits.fmtMoneyString(resp.data.rdp)
        this.setData({
          dtimes: resp.data.times,
          formatter: (day) => {
            day.topInfo = '￥ ' + rdpStr

            let today = new Date()
            today = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0)
            for (let i = 0; i < this.data.dtimes.length; i++) {
              if (day.date.getTime() >= today.getTime() && day.date.getTime() >= this.data.dtimes[i].begin && day.date.getTime() <= this.data.dtimes[i].close) {
                day.type = 'disabled'
                if (this.data.dtimes[i].type == 'Rent') {
                  day.bottomInfo = '已租'
                } else if (this.data.dtimes[i].type == 'Stop') {
                  day.bottomInfo = '停租'
                } else {
                  day.bottomInfo = '过期'
                }
                return day
              }
            }

            // if (day.type === 'middle' || day.type === 'start' || day.type === 'start-end' || day.type === 'end') {
            //   day.topInfo = '￥ ' + rdpStr
            // }
            if (day.type === 'start') {
              day.bottomInfo = '开始'
            } else if (day.type === 'end') {
              day.bottomInfo = '结束'
            } else if (day.type === 'start-end') {
              day.bottomInfo = '开/结'
            }
            return day
          },
          add: true,

        })
        for (let i = 0; i < this.data.dtimes.length; i++) {
          if (this.data.begin.value >= this.data.dtimes[i].begin && this.data.begin.value <= this.data.dtimes[i].close ||
            this.data.close.value >= this.data.dtimes[i].begin && this.data.close.value <= this.data.dtimes[i].close) {
            this.doSetDatetime(undefined, undefined)
          }
        }
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false
        })
      }
    })
  },
  hideAdd() {
    this.setData({
      add: false
    })
  },
  doDateChange(e) {
    let sday = e.detail[0]
    let eday = e.detail[1]
    if (!eday) {
      return
    }
    let begin = new Date(sday.getFullYear(), sday.getMonth(), sday.getDate(), 0, 0, 0, 0)
    let close = new Date(eday.getFullYear(), eday.getMonth(), eday.getDate(), 0, 0, 0, 0)

    this.doSetDatetime(begin, begin.getTime() >= close.getTime() ? Kits.getNextTime(begin.getTime()) : close)
  },
  doReset() {
    let now = new Date()
    let begin = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0)

    this.doSetDatetime(begin, begin.addDays(1))
  },

  // 设置日期时间
  doSetDatetime(beginTime, closeTime) {
    if (beginTime == undefined && closeTime == undefined) {
      this.setData({
        range: [undefined, undefined],

        begin: {
          value: undefined,
          year: '',
          date: '取车日期',
          time: '',
        },
        close: {
          value: undefined,
          year: '',
          date: '还车日期',
          time: '',
        },
        hdays: '请选择日期'
      })
      return
    }
    let begin = {
      value: beginTime.getTime(),
      year: beginTime.format('yyyy'),
      date: beginTime.format('MM月dd日'),
      time: beginTime.format('HH:mm'),
    }
    let close = {
      value: closeTime.getTime(),
      year: closeTime.format('yyyy'),
      date: closeTime.format('MM月dd日'),
      time: closeTime.format('HH:mm'),
    }
    this.setData({
      minDate: new Date().getTime(),
      maxDate: new Date().getTime() + 90 * 24 * 60 * 60 * 1000,
      begin: begin,
      close: close,
      hdays: '共' + Kits.getDateMemo(beginTime, closeTime),
      range: [begin.value, close.value],
    })
  },
  doAdd() {
    this.execSave(1, new Date(this.data.begin.value).format("yyyy-MM-dd"), new Date(this.data.close.value).format("yyyy-MM-dd"))
  },
  doDelete(e) {
    let conf = e.currentTarget.dataset.conf
    Util.Dialog.confirm(`是否删除设置？`, () => {
      this.execSave(2, conf.begin, conf.close)
    })
  },
  execSave(type, begin, close) {
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/merchant/car/stop/set',
      body: {
        id: this.data.cid,
        type: type,
        begin: begin,
        close: close
      },
      fnSucc: () => {
        this.execLoad()
        if (type == 1) {
          Util.Toast.succ('保存成功')
          this.setData({
            add: false
          })
        } else {
          Util.Toast.succ('删除成功')
        }
      }
    })
  },

})